<template>
  <div>
    <!--    <div v-if="inClass">-->
    <!--      <router-view></router-view>-->
    <!--    </div>-->
    <main-header :role="role" @changeMenu="changeMenu"></main-header>
    <div v-show="Menu === '1'">
      <main-top-card :role="role"></main-top-card>
      <main-middle :role="role" @changeStudy="changeStudy"></main-middle>
      <div v-show="role === 'teacher'">
        <div v-show="study === 'first'">
          <my-class :myCourse="myTeachCourse"></my-class>
        </div>
        <div v-show="study === 'second'">
          <my-class :myCourse="myStudyCourse" v-if="update"></my-class>
        </div>
        <div v-show="study === 'third'">
          <my-class :myCourse="myHelpCourse"></my-class>
        </div>
      </div>
      <div v-show="role==='student'">
        <div v-show="study === 'first'">
          <my-class :myCourse="myStudyCourse"></my-class>
        </div>
        <div v-show="study === 'second'">
          <my-class :myCourse="myHelpCourse"></my-class>
        </div>
      </div>
    </div>
    <div v-show="Menu === '2'">
      <h1 style="text-align: center">备考区</h1>
    </div>
    <div v-show="Menu === '3'">
      <h1 style="text-align: center">虚拟教研室</h1>
    </div>
  </div>
</template>

<script>

import MainHeader from "@/components/mainpage/MainHeader.vue";
import MainTopCard from "@/components/mainpage/MainTopCard.vue";
import MainMiddle from "@/components/mainpage/MainMiddle.vue";
import MyClass from "@/components/mainpage/MyClass.vue";

export default {
  data() {
    return {
      update: true,
      role: '',
      Menu: '1',
      study: 'first',
      inClass: false,
      myStudyCourse: [],
      myHelpCourse: [],
      myTeachCourse: []
    }
  },
  components: {
    MainHeader,
    MainTopCard,
    MainMiddle,
    MyClass
  },

  mounted() {
    this.$router.push('/main')
    this.role = localStorage.getItem("role");
    if (this.role === null) {
      this.$router.push('/login')
    }
    this.getStudyCourse()
    this.getTeachCourse()
    this.getHelpCourse()
    this.$bus.$on('refresh', (data) => {
      if (data) {
        this.getStudyCourse()
        this.getTeachCourse()
        this.getHelpCourse()
      }
    })
  },
  created() {

  },
  methods: {
    getStudyCourse() {
      this.request.post("/getStudyCourse", {
        username: localStorage.getItem("username"),
        role: localStorage.getItem("role"),
      }).then((res) => {
        if (res === null) {
          this.$message.error("没有获取到课程")
        }else {
          this.myStudyCourse = []
          if (res instanceof Array){
            this.dealWithCourse(res).forEach((item) => {
              this.myStudyCourse.push(item)
            })
          }
        }
        // for (let i = 0; i < res.length - 1; i++) {
        //   let done = true;
        //   for (let j = 0; j < res.length - 1 - i; j++) {
        //     if (parseInt(res[j].courseTime.slice(0, 4)) < parseInt(res[j + 1].courseTime.slice(0, 4))) {
        //       let temp = res[j];
        //       res[j] = res[j + 1];
        //       res[j + 1] = temp;
        //       done = false;
        //     }
        //   }
        //   if (done) {
        //     break;
        //   }
        // }


        // if (res.length === 0) {
        //   this.$message('登录验证失效，即将返回登录页面')
        //   this.$router.push('/login')
        //   localStorage.removeItem('token')
        //   localStorage.removeItem("role")
        //   localStorage.removeItem("username")
        // }
      })
    },
    getTeachCourse() {
      this.request.post("/getTeachCourse", {
        username: localStorage.getItem("username"),
        role: localStorage.getItem("role"),
      }).then((res) => {

        if (res === null) {
          this.$message.error("没有获取到课程")
        }
        this.myTeachCourse = this.dealWithCourse(res)
      })
    },
    getHelpCourse() {
      this.request.post("/getHelpCourse", {
        username: localStorage.getItem("username"),
        role: localStorage.getItem("role"),
      }).then((res) => {
        if (res === null) {
          this.$message.error("没有获取到课程")
        }
        this.myHelpCourse = this.dealWithCourse(res)
      })
    },
    changeMenu(value) {
      this.Menu = value
    },
    changeStudy(value) {
      this.study = value
    },
    dealWithCourse(res) {
      for (let i = 0; i < res.length - 1; i++) {
        let done = true;
        for (let j = 0; j < res.length - 1 - i; j++) {
          if (parseInt(res[j].courseTime.slice(0, 4)) < parseInt(res[j + 1].courseTime.slice(0, 4))) {
            let temp = res[j];
            res[j] = res[j + 1];
            res[j + 1] = temp;
            done = false;
          }
        }
        if (done) {
          break;
        }
      }
      return res;
    },
  },
  watch: {
    // myStudyCourse(newValue, oldValue) {
    //   // console.log(newValue)
    //   // console.log(oldValue)
    // }
  }

}
</script>

<style scoped>

</style>
